package com.zsr.chapter05.bean;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author zsr
 * @Description
 * @Date 2021/4/22 10:16
 */
public class BookDB {
    private static Map<String,Book> books=new LinkedHashMap<String, Book>();

    static {
        books.put("1",new Book("1","javaweb开发"));
        books.put("2",new Book("2","jdbc开发"));
        books.put("3",new Book("3","java基础"));
        books.put("4",new Book("4","struts开发"));
        books.put("5",new Book("5","spring开发"));
    }
    //获取所有的图书
    public static Collection<Book> getAll(){
        return books.values();
    }
    //根据id获取图书
    public static Book getBook(String id){
        return books.get(id);
    }
    //根据id删除图书
    public static void deleteBook(String id){
        books.remove(id);
    }
    //添加图书
    public static void addBook(Book book){
        //判断Map中是否有相同key
        if(book!=null&& !books.containsKey(book.getId())){
            books.put(book.getId(),book);
        }
    }
}
